package com.sunther.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunther.document.dao.RoleTagMiddleDao;
import com.sunther.document.entity.ProjectEntity;
import com.sunther.document.entity.RoleTagEntity;
import com.sunther.document.entity.RoleTagMiddleEntity;
import com.sunther.document.service.RoleTagMiddleService;
import com.sunther.document.service.RoleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author csb
 * @description: TODO
 * @date 2023/5/16 15:52
 */
@Service
public class RoleTagMiddleServiceImpl extends ServiceImpl<RoleTagMiddleDao, RoleTagMiddleEntity> implements RoleTagMiddleService {

    @Autowired
    private RoleTagService roleTagService;

    @Override
    public List<Integer> getTagIdByRoleId(Integer roleId) {
        QueryWrapper<RoleTagMiddleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<RoleTagMiddleEntity> listByRoleId = list(wrapper);
        List<Integer> collect = listByRoleId.stream().map(RoleTagMiddleEntity::getRoleTagId).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Map<Integer, List<Integer>> getRoleListMap() {
        List<RoleTagMiddleEntity> list = list();
        Map<Integer, List<Integer>> collect = new HashMap<>();
        for (RoleTagMiddleEntity roleTagMiddleEntity : list) {
            collect.computeIfAbsent(roleTagMiddleEntity.getRoleId(), k -> new ArrayList<>()).add(roleTagMiddleEntity.getRoleTagId());
        }
        return collect;
    }

    @Override
    public Set<Integer> getTagIdByRoleIdNotLevelOne(Integer roleId) {

        QueryWrapper<RoleTagEntity> wrap = new QueryWrapper<RoleTagEntity>();
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(0, 5));
        wrap.in("parent_id",list);
        List<Integer> ints = roleTagService.list(wrap).stream().map(RoleTagEntity::getId).collect(Collectors.toList());

        QueryWrapper<RoleTagMiddleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        wrapper.notIn("role_tag_id",ints);
        List<RoleTagMiddleEntity> listByRoleId = list(wrapper);
        Set<Integer> collect = listByRoleId.stream().map(RoleTagMiddleEntity::getRoleTagId).collect(Collectors.toSet());
        return collect;
    }
}
